package com.xqh.base.util.word;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpecialCharUtils {

    /**
     * 用于 分割 内容上传 的正则
     */
    public static Pattern pattern = Pattern.compile("(\\[[^\\]]*\\])");

    public static String removeSpecialChar(String word){
        String encodeChar = encode(word).replaceAll("%C2%A0","");
        return decode(encodeChar).trim();
    }

    public static String encode(String word){
        try {
            return URLEncoder.encode(word, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return word;
    }

    public static String decode(String word){
        try {
            return URLDecoder.decode(word, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return word;
    }

    /**
     * 将 excel中 [包含的内容] 解析出来
     * @param value
     * @return
     */
    public static List<String> matchExcelData(String value){
        List<String> list = new ArrayList<>();
        Matcher matcher = pattern.matcher(value);
        while (matcher.find()){
            String g = matcher.group();
            list.add(g.substring(1, g.length()-1));
        }
        return list;
    }

    public static void main(String[] args) {
        String abc = "[asdfasdf][gggggghhh][hherthrty]";
        System.out.println(matchExcelData(abc));
    }
}
